package com.wujunchao.demo.service.impl;

import com.wujunchao.demo.pojo.Student;
import com.wujunchao.demo.pojo.Teacher;
import com.wujunchao.demo.mapper.TeacherMapper;
import com.wujunchao.demo.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 老师表(Teacher)表服务实现类
 *
 * @author wn
 * @version 1.0
 * @since 2021-07-02 15:10:26
 */
@Service("teacherService")
@SuppressWarnings("all")
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    /**
     * 根据模糊条件查询总个数
     *
     * @return 返回查询到的总个数
     */
    @Override
    public Map<String, Object> chaXunCount(String mingCheng) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("list", this.teacherMapper.chaXunCount(mingCheng));
        return map;
    }

    @Override
    public boolean login(String username, String password) {
        Teacher login = teacherMapper.login(username, password);
        if (login!=null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 查询所有数据
     *
     * @return 返回所有数据
     */
    @Override
    public Map<String, Object> chaXunAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", this.teacherMapper.chaXunAll());
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param tno 主键
     * @return 实例对象
     */
    public Map<String, Object> chaXunById(Integer tno) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("obj", this.teacherMapper.chaXunById(tno));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param page      查询起始位置
     * @param mingCheng 查询条数
     * @return 对象列表
     */
    @Override
    public Map<String, Object> chaXunFenYe(int page, String mingCheng) {
        // 获取当前表中的总记录
        int tableCount = this.teacherMapper.chaXunCount(mingCheng);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / 10 + 1;
        // 计算每页开始的下标值
        int xiaBiao = (page - 1) * 10;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.teacherMapper.chaXunFenYe(xiaBiao, mingCheng));
        return map;
    }

    /**
     * 新增数据
     *
     * @param teacher 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String, Object> xinZeng(Teacher teacher) {
        this.teacherMapper.xinZeng(teacher);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param teacher 实例对象
     * @return 实例对象
     */
    @Override
    public Map<String, Object> gengXinById(Teacher teacher) {
        this.teacherMapper.gengXinById(teacher);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param tno 主键
     * @return 是否成功
     */
    @Override
    public Map<String, Object> shanChuById(String tno) {
        this.teacherMapper.shanChuById(tno);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }

    @Override
    public Map<String, Object> updateStudentNumber(Teacher teacher) {
        int i = this.teacherMapper.updateStudentNumber(teacher);
        Map<String,Object> map=new HashMap<>();
        map.put("code",200);
        return map;
    }
}
